#!/usr/bin/env python
# -*- coding:utf-8 -*-
import datetime

import flask
from flask_restful import Resource, reqparse
from flask import jsonify
from utils.JwtMiddleware import TokenRequire
from controller.lessonController import LessonController
from utils import commons
from utils.response_code import RET, error_map_EN
from utils.responseParser import ResponseParser
from utils.myLogging import logger
from werkzeug.exceptions import BadRequest
from service.lessonService import LessonService


def get_list(date):
    return datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S").timestamp()


# 修改班级
class LessonUpdateResource(Resource):
    @classmethod
    @TokenRequire
    def update(cls, lesson_id):
        if not lesson_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('is_delete', location='form', required=False, help='is_delete参数类型不正确或缺失')
        parser.add_argument('lesson_name', location='form', required=True, help='lesson_name参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['lesson_id'] = lesson_id

        res = LessonController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])


# 添加班级
class LessonAddResource(Resource):
    @classmethod
    @TokenRequire
    def post(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('lesson_name', location='form', required=True, help='lesson_name参数类型不正确或缺失')
        try:
            data = parser.parse_args()
            data = commons.put_remove_none(**data)
            res = LessonService.add(**data)
            res.get('data') == res.get('data').update(data)
            if res.get("code") != RET.OK:
                logger.error(res.get("data").get("error"))
                return jsonify({
                    "code": res.get("code"),
                    "error": res.get("data").get("error"),
                    "message": "新增班级失败",
                })
            logger.info(f"user {res.get('data').get('OrderID')} add success")
            return jsonify({
                "code": RET.OK,
                "message": "新增班级成功",
                "data": res,
            })
        except BadRequest as e:
            logger.error(str(e))
            return jsonify(ResponseParser.parse_param_error(error=str(e)))
        except Exception as e:
            logger.warning(str(e))
            return jsonify(ResponseParser.parse_unknown_error(error=str(e)))


# 删除班级
class LessonDelResource(Resource):
    @classmethod
    @TokenRequire
    def delete(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('lesson_id', location='form', required=True, help='lesson_id参数类型不正确或缺失')
        try:
            data = parser.parse_args()
            data = commons.put_remove_none(**data)
            res = LessonService.delete(lesson_id=data['lesson_id'])
            res['data']['lesson_id'] = res['data']['lesson_id'][0]
            if res.get("code") != RET.OK:
                logger.error(res.get("data").get("error"))
                return jsonify({
                    "code": res.get("code"),
                    "error": res.get("data").get("error"),
                    "message": res.get("message"),
                })

            logger.info("delete risky note success")
            return jsonify(res)
        except BadRequest as e:
            logger.error(str(e))
            return jsonify(ResponseParser.parse_param_error(error=str(e)))
        except Exception as e:
            logger.warning(str(e))
            return jsonify(ResponseParser.parse_unknown_error(error=str(e)))